package sz.szsmk.citizencard.ui.adapter;

import android.os.Bundle;
import androidx.viewpager.widget.ViewPager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.RoutineHeadItemBinding;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.bean.response.QueryUserCardResponse;
import sz.szsmk.citizencard.ui.activity.HomeRoutineCardActivity;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/16 16:09
 * description:
 */
public class RoutineHeadAdapter extends DelegateAdapter.Adapter<RoutineHeadAdapter.RoutineHeadHolder> {
    private HomeRoutineCardActivity mContext;
    private ArrayList<ArrayList<QueryUserCardResponse.DataBean>> mendList;

    private LayoutHelper mDefaultLayoutHelper;
    private List<QueryUserCardResponse.DataBean> data;

    private ArrayList<QueryUserCardResponse.DataBean> cardInfo;
    private ViewPager cardInfoVp;

    private List<View> viewList;
    private View view;

    public RoutineHeadAdapter(HomeRoutineCardActivity context, DefaultLayoutHelper headHelper) {
        mContext = context;
        mDefaultLayoutHelper = headHelper;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mDefaultLayoutHelper;
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.routine_head_item;
    }

    @Override
    public RoutineHeadHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return RoutineHeadHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(RoutineHeadHolder holder, int position) {
        loadItemData(position, holder.binding);
    }

    private void loadItemData(int position, RoutineHeadItemBinding binding) {
//        if (data!=null){
//            viewList = new ArrayList<View>();
//        TextView bjTv = binding.titleLayout.bjTv;
//        binding.titleLayout.settingTitle.setText("市民卡");
//        binding.titleLayout.settingTitle.setTextColor(mContext.getResources().getColor(R.color.white));
//        bjTv.setVisibility(View.VISIBLE);
//        bjTv.setText("交易记录");
//        bjTv.setTextColor(mContext.getResources().getColor(R.color.white));
//        bjTv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                JumpUtils.jump(mContext,"trans",0);
//            }
//        });
//        binding.titleLayout.bjTv.setTextColor(mContext.getResources().getColor(R.color.white));
//        binding.titleLayout.back.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mContext.finish();
//            }
//        });
//
//        }
        binding.cardRecharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String access_token = ACache.get(mContext).getAsString("access_token");

                Bundle bundle = new Bundle();
                L.e(Constant.TAG, "云闪充access_token====" + access_token);
                bundle.putString("url", "https://newapp.szsmk.com/app-front-end/#/charge/main?access_token=" + access_token);
                JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
            }
        });
        binding.cardSpeciTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JumpUtils.jump(mContext, "specialpsd", 0);
            }
        });
    }


    @Override
    public int getItemCount() {
        return 1;
    }


    public static List<String> removeDuplicate(ArrayList<String> titles) {
        Set set = new LinkedHashSet<String>();

        set.addAll(titles);
        titles.clear();
        titles.addAll(set);
        return titles;
    }

    public void setViewData(List<QueryUserCardResponse.DataBean> data) {
        this.data = data;

        if (data != null) {
            ArrayList<String> titles = new ArrayList<>();
            for (int i = 0; i < data.size(); i++) {
                titles.add(data.get(i).getCardNo());
            }
            List<String> newList = removeDuplicate(titles);

            mendList = new ArrayList<>();

            for (int i = 0; i < newList.size(); i++) {
                String cardNo = newList.get(i);

                //卡片详情集合
                cardInfo = new ArrayList<>();

                mendList.add(cardInfo);

                for (int j = 0; j < data.size(); j++) {
                    QueryUserCardResponse.DataBean dataBean = data.get(j);
                    String cardNo1 = dataBean.getCardNo();
                    if (cardNo.equals(cardNo1)) {
                        cardInfo.add(dataBean);
                    }

                }
            }
        }


    }

    public static class RoutineHeadHolder extends RecyclerView.ViewHolder {

        private RoutineHeadItemBinding binding;

        public static RoutineHeadHolder creat(ViewGroup parent) {
            RoutineHeadItemBinding binding = RoutineHeadItemBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new RoutineHeadHolder(binding);
        }


        public RoutineHeadHolder(RoutineHeadItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
